load("02_11_m5s_user_activity_univ_only_timeseries.RData")

cor.mtest <- function(mat, conf.level = 0.95) {
  mat <- as.matrix(mat)
  n <- ncol(mat)
  p.mat <- lowCI.mat <- uppCI.mat <- matrix(NA, n, n)
  diag(p.mat) <- 0
  diag(lowCI.mat) <- diag(uppCI.mat) <- 1
  for (i in 1:(n - 1)) {
    for (j in (i + 1):n) {
      tmp <- cor.test(mat[, i], mat[, j], conf.level = conf.level)
      p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
      lowCI.mat[i, j] <- lowCI.mat[j, i] <- tmp$conf.int[1]
      uppCI.mat[i, j] <- uppCI.mat[j, i] <- tmp$conf.int[2]
    }
  }
  return(list(p.mat, lowCI.mat, uppCI.mat))
}

nms <- names(unique_id_timeseries)
nms <- nms[nms!='blog_post']

sequence <- seq(as.Date('2005-01-01'), as.Date('2015-01-24'), by=7)

ts_matrix <- data.frame(from=as.character(sequence)) 
                                 
for (what in nms) {
  ts_matrix <- merge(ts_matrix, as.data.frame(unique_id_timeseries[[what]][['ts']]), by="from", all.x=TRUE)
}

names(ts_matrix)[2:14] <- nms


require(corrplot)
M1 <- cor(ts_matrix[2:14], use = "pairwise.complete.obs")
# res1 <- cor.mtest(ts_matrix[ts_matrix$from!="2013-02-23",2:14], 0.95)
# corrplot.mixed(M1)
#diag(M1) <- 0
# corrplot(M1, p.mat = res1[[1]], sig.level = 0.0001)

require(forecast)
require(lmtest)
# Facebook <-> Meetup 
mu <- diff(ts_matrix$meetup[!is.na(ts_matrix$facebook) & !is.na(ts_matrix$meetup)])
fb <- diff(ts_matrix$facebook[!is.na(ts_matrix$facebook) & !is.na(ts_matrix$meetup)])
grangertest(mu ~ fb, order=1) # p = 0.004518 **
grangertest(fb ~ mu, order=1) # p = 0.8948
mu <- diff(ts_matrix$meetup[!is.na(ts_matrix$facebook_comment) & !is.na(ts_matrix$meetup)])
fb <- diff(ts_matrix$facebook_comment[!is.na(ts_matrix$facebook_comment) & !is.na(ts_matrix$meetup)])
grangertest(mu ~ fb, order=1) # p = 0.461
grangertest(fb ~ mu, order=1) # p = 0.3843
# Blog <-> Facebook
bg <- diff(ts_matrix$blog[!is.na(ts_matrix$facebook) & !is.na(ts_matrix$blog)])
fb <- diff(ts_matrix$facebook[!is.na(ts_matrix$facebook) & !is.na(ts_matrix$blog)])
grangertest(bg ~ fb, order=1) # p = 1.638e-11 ***
grangertest(fb ~ bg, order=1) # p = 7.612e-07 ***
bg <- diff(ts_matrix$blog[!is.na(ts_matrix$facebook_comment) & !is.na(ts_matrix$blog)])
fb <- diff(ts_matrix$facebook_comment[!is.na(ts_matrix$facebook_comment) & !is.na(ts_matrix$blog)])
grangertest(bg ~ fb, order=1) # p = 2.048e-08 ***
grangertest(fb ~ bg, order=1) # p = 3.906e-07 ***
# Forum <-> Meetup
mu <- diff(ts_matrix$meetup[!is.na(ts_matrix$forum) & !is.na(ts_matrix$meetup)])
fr <- diff(ts_matrix$forum[!is.na(ts_matrix$forum) & !is.na(ts_matrix$meetup)])
grangertest(mu ~ fr, order=1) # p = 0.9434
grangertest(fr ~ mu, order=1) # p = 1.41e-05 ***
# Forum <-> Facebook
fb <- diff(ts_matrix$facebook[!is.na(ts_matrix$forum) & !is.na(ts_matrix$facebook)])
fr <- diff(ts_matrix$forum[!is.na(ts_matrix$forum) & !is.na(ts_matrix$facebook)])
grangertest(fb ~ fr, order=1) # p = 0.004194 **
grangertest(fr ~ fb, order=1) # p = 2.2e-16 ***
fb <- diff(ts_matrix$facebook_comment[!is.na(ts_matrix$forum) & !is.na(ts_matrix$facebook_comment)])
fr <- diff(ts_matrix$forum[!is.na(ts_matrix$forum) & !is.na(ts_matrix$facebook_comment)])
grangertest(fb ~ fr, order=1) # p = 0.007292 **
grangertest(fr ~ fb, order=1) # p = 1.011e-09 ***
# Forum <-> Blog
bg <- diff(ts_matrix$blog[!is.na(ts_matrix$forum) & !is.na(ts_matrix$blog)])
fr <- diff(ts_matrix$forum[!is.na(ts_matrix$forum) & !is.na(ts_matrix$blog)])
grangertest(bg ~ fr, order=1) # p = 0.05124 .
grangertest(fr ~ bg, order=1) # p = 5.767e-08 ***
# Meetup <-> Blog
bg <- diff(ts_matrix$blog[!is.na(ts_matrix$forum) & !is.na(ts_matrix$blog)])
mu <- diff(ts_matrix$meetup[!is.na(ts_matrix$forum) & !is.na(ts_matrix$blog)])
grangertest(bg ~ mu, order=1) # p = 0.9748
grangertest(mu ~ bg, order=1) # p = 0.5831

# First national assembly
line1 = as.numeric(as.Date("2009-03-08"))
# First regional election
line2 = as.numeric(as.Date("2010-03-29"))
# Local elections
line3 = as.numeric(as.Date("2011-05-16"))
# Local elections
line4 = as.numeric(as.Date("2012-05-07"))
# Sicilian elections
line5 = as.numeric(as.Date("2012-10-28"))
# National elections
line6 = as.numeric(as.Date("2013-02-25"))
# V-Day
line7 = as.numeric(as.Date("2007-09-08"))
# European election
line8 = as.numeric(as.Date("2014-05-22"))
# Regional election
line9 = as.numeric(as.Date("2014-02-16"))


myplot <- function(i) {
  n <- unique_id_timeseries[[i]][['n']]
  ggplot(unique_id_timeseries[[i]][['ts']], aes(as.Date(from), active_users)) + 
    scale_x_date(limits=c(as.Date("2005-01-01"),as.Date("2015-02-01")))+
    geom_line() +
    labs(title=i,
         y="Users", x=paste0("n=", format(n, format="d", big.mark=','))) +
    geom_vline(xintercept = line4) +
    geom_vline(xintercept = line6) +
    geom_vline(xintercept = line7) +
    geom_vline(xintercept = line8) +
    geom_vline(xintercept = line9)
}

p <- lapply(c("blog","meetup","facebook_post", "facebook_comment","facebook_like", "forum"),
            myplot)
do.call("grid.arrange", c(p, ncol=1))
